var mainService = angular.module('chargingApp.chargingService', []);


mainService.config(function($httpProvider) {
		$httpProvider.interceptors.push([
			'$injector',
			function($injector) {
				return $injector.get('AuthInterceptor');
			}
		]);
	})
	.factory('AuthInterceptor', function($rootScope, $q) {
		return {
			response: function(response) {
				// 这里判断完成之后，重定向一下;
				return response;
			},
			responseError: function(response) {

				switch (response.status) {
					case 401:
						console.log('走到了401');
						window.location.reload();
						break
					default:
						console.log('走到了default');
				}
				return $q.reject(response);
			}
		};
	})


mainService.factory('commonService', function($q, $http) {
	var factory = {};
	factory.commonAjaxByGet = function(url) {
		var deferred = $q.defer();
		$http.get("/oms" + url).success(function(data, status, headers, config) {
			deferred.resolve(data);
		}).error(function(data, status, headers, config) {});
		return deferred.promise;
	};
	factory.commonAjaxByPost = function(url, postData) {
		var deferred = $q.defer();
		$http({
			method: 'POST',
			url: "/oms" + url,
			data: postData,
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
			},
			cache: false
		}).success(function(data, status, headers, config) {
			deferred.resolve(data);
		});
		return deferred.promise;
	};
	factory.commonDownlodaByPost = function(url, postData) {
		var deferred = $q.defer();
		$http({
			method: 'POST',
			url: "/oms" + url,
			data: postData,
			headers: {
				'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8'
			},
			cache: false,
			responseType: 'arraybuffer'
		}).success(function(data, status, headers, config) {
			deferred.resolve(data);
		});
		return deferred.promise;
	};

	return factory;
})



mainService.service('chargingService', function(commonService) {
	return {
		getBigScreenByChargerId: function() {
			var url = "/getBigScreenByChargerId" + location.search;
			return commonService.commonAjaxByGet(url);
		},
	getWeatherByCityId:function(){
      var url = "/getWeatherByCityId?cityId=CN101121501";
      return commonService.commonAjaxByGet(url);
    }
	};
})